#version 430 core

#define BLOCK_COUNT 200

layout (std140) uniform UniformParams {
	mat4 projection;
	mat4 view;
	mat4 viewProjMat;
	mat4 biasMat;
	mat4 shadowProjection;
	mat4 worldToLightMat;
	vec4 camPosition;
	vec4 lightDir;
} uParams;

layout (std140) uniform ShaderParams
{
	mat4 model[BLOCK_COUNT];
}params;

 
layout(location = 0) in vec3 position;
layout(location = 1) in unsigned int id;

layout(location = 2) in vec3 normal;
layout(location = 3) in vec2 texCoord;

out vec3 lightDir;
out vec3 viewDir;
out vec3 normalDir;
out vec4 shadowCoord;

out vec2 uv;

void main(){
	
	//get position in clip space and give it to fragment shader
	mat4 model = params.model[id];
    vec4 worldPos = model * vec4(position,1);
    gl_Position = uParams.viewProjMat * worldPos;

	//perform everything in view space
	vec3 posViewSpace = (uParams.view * worldPos).xyz;
	viewDir = normalize(-posViewSpace);
	lightDir = normalize((uParams.view * -uParams.lightDir)).xyz;

	//calculate normal matrix
	mat4 normalMatrix = transpose(inverse(uParams.view * model));
	normalDir = normalize(normalMatrix * vec4(normal.x, normal.y, normal.z, 1.0)).xyz;

	//set uv
	uv = texCoord;

	//shadow coordinate
	shadowCoord = uParams.biasMat * uParams.shadowProjection * uParams.worldToLightMat * worldPos;
}